# Figure 4: Italian Public Discourse Trends About Chinese People

rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(zoo)
library(ggplot2)

articles <- fread('./data/newspaper_articles_sentiment_classification.csv')

articles[,month:=as.yearmon(paste(month(date), year(date), sep='-'), format='%m-%Y')]
articles[group=='china', region:='Chinese people']
articles[group=='africa', region:='African people']
articles[,label:=ifelse(month=="2018-09-01",as.character(region),NA)]

# Figure 4 (b)
ggplot(articles[,.(share=mean(sentiment_negative)), by=.(month, group, label)], aes(month, share, color = group)) +
  geom_line(size=1) +
  scale_colour_grey(start = 0.2, end = 0.6) +
  geom_label(aes(label = label), nudge_x = 0.35, size = 4) +
  ylab("Share of negative newspaper articles") +
  xlab(NULL) +
  geom_vline(xintercept = as.yearmon('01-2020', '%m-%Y'), color='blue', linetype='dashed') +
  geom_text(x=as.yearmon('10-2019', '%m-%Y'), y=.95, label="Covid-19", color='blue') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position ='none',
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)), axis.title.x = element_text(size = rel(1.1)))
ggsave('./paper_share_trends.pdf', width=6, height=4.5)

# Figure 4 (a)
articles_plot <- melt(articles[,.(`Total articles`=.N, `Negative articles`=sum(sentiment_negative)), by=.(month, group)],
                      id.vars = c('month', 'group'), 
                      measure.vars = c('Total articles', 'Negative articles'))

ggplot(articles_plot, 
       aes(month, value, color = group, linetype=variable)
) +
  geom_line(size = 1) +
  scale_colour_grey(start = 0.2, end = 0.6, labels = c(paste("African", "people", sep='\n'), paste("Chinese", "people", sep='\n'))) +
  geom_vline(xintercept = as.yearmon('01-2020', '%m-%Y'), color='blue', linetype='dashed') +
  geom_text(x=as.yearmon('10-2019', '%m-%Y'), y=800, label="Covid-19", color='blue') +
  ylab("Total and negative newspaper articles") +
  xlab(NULL) +
  scale_y_continuous(breaks=seq(200,1400, 200)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position ='top',
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)), axis.title.x = element_text(size = rel(1.1)))
ggsave('./paper_total_negative_trends.pdf', width=6, height=4.5)
